'use strict';

var utils = require('./utils');
var moment = require('moment');
var regx = (/\.avi$|\.rmvb$|\.rm$|\.asf$|\.divx$|\.mpg$|\.mpeg$|\.mpe$|\.wmv$|\.mp4$|\.mkv$|\.vob$/i);
var Parser = function () {
    this.address = null;
    this.infohash = null;
    this.magnet = null;
    this.port = 0;
    this.files = null;
    this.name = null;
    //this.parseName(data);
    this.size = 0;
    //this.parseSize(data);
    //人类可读的大小KB/MB/GB/PB
    this.humanSize = null;
    //this.parseHumanSize(size);
    this.createDate = null;
    this.lastUpdate = null;
    this.visitCount = 1;
    this.isVideo = false;
};

Parser.prototype.parse = function (data) {
    this.address = data.address;
    this.infohash = data.infohash;
    this.magnet = data.magnet;
    this.port = data.port;
    this.files = [];
    this.name = null;
    //this.parseName(data);
    this.size = 0;
    //this.parseSize(data);
    //人类可读的大小KB/MB/GB/PB
    this.humanSize = '0 B';
    //this.parseHumanSize(size);
    this.createDate = moment().format('YYYY-MM-DD');
    this.lastUpdate = moment().format('YYYY-MM-DD');
    this.visitCount = 1;
    this.isVideo = false;
    this.parseFiles(data);
    return this;
};
Parser.prototype.parseFiles = function (data) {
    var tfile, tsize, maxfile, maxsize = 0;
    if (data.info.files != null && data.info.files != undefined) {
        data.info.files.forEach(function (file) {
            try{
		tfile = file.path[0].toString('UTF-8');
            }catch (error){
		return;
	    }

            if (this.startWith(tfile, '_____padding_file')) {
                return;
            }
            if(tfile===''||tfile.length===0)
            {
                return;
            }
            tsize = file.length;
            this.size += tsize;
            if (tsize > maxsize) {
                maxsize = tsize;
                maxfile = tfile;
            }
            if(tfile == undefined|| tfile == '')
		{
			return;
		}
            var tobj = { file: tfile, size: tsize, hsize: this.calcHumanSize(tsize) };
            this.files.push(tobj);
            //验证是否是视频
            if (!this.isVideo) {
                this.isVideo = regx.test(tfile);
            }
        }.bind(this));
        //文件名称
        if (data.info.name != undefined) {
            try {
                this.name = data.info.name.toString('UTF-8');
            } catch (error) {
                console.log("error:" + data.info.name);
                console.log(error);
            }

        } else {
            this.name = maxfile;
        }
    } else {
        tfile = data.info.name.toString('UTF-8');
        tsize = data.info.length;
        var tobj = { file: tfile, size: tsize, hsize: this.calcHumanSize(tsize) };
        this.files.push(tobj);
        this.name = tfile;
        this.size = tsize;
        this.isVideo = regx.test(tfile);
    }
    this.humanSize = this.calcHumanSize(this.size);
};

Parser.prototype.calcHumanSize = function (total) {
    if (total < 1024) {
        return total + " B";
    }
    total = total / 1024;
    if (total < 1024) {
        return total.toFixed(1) + " KB";
    }
    total = total / 1024;
    if (total < 1024) {
        return total.toFixed(1) + " MB";
    }
    total = total / 1024;
    if (total < 1024) {
        return total.toFixed(1) + " GB";
    }
    total = total / 1024;
    return total.toFixed(1) + " PB";
};
Parser.prototype.startWith = function (src, des) {
    var reg = new RegExp("^" + des);
    return reg.test(src);
};

Parser.prototype.push = function (node) {
    if (this.nodes.length >= this.maxsize) {
        return;
    }
    this.nodes.push(node);
};

module.exports = Parser;
